package com.playball.lottery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Parcel;
//modify by rain remove jPush
//import cn.jpush.android.api.JPushInterface;

import com.playball.lottery.eventmanger.EventManager;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.nt.MyTask;
import com.playball.lottery.util.LotConstants;

public class MainService extends Service implements LotConstants {


	public static ArrayList<MyTask> allTask = new ArrayList<MyTask>();
	// for receive customer msg from jpush server
	private MessageReceiver mMessageReceiver;
	public static final String MESSAGE_RECEIVED_ACTION = "com.playball.lottery.MESSAGE_RECEIVED_ACTION";

	private Map<String, String> map;

	// 添加任务
	public void newTask(MyTask task) {
		allTask.add(task);
	}

	private Handler hand = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {

			default:

				break;
			}
		}

	};

	// 执行任务
	public void doTask(MyTask task) {
		String strResult;
		String param = new String();
		// 设置请求action
		String action = task.getParms()[0];
		// 获取请求返回值
		strResult = GetRequestParams.getRequestResult(param, action, getApplicationContext(), false);
		// System.out.println("strResult:" + strResult);
		EventManager.getInstance().onEvent(task.getTaskId(), strResult);
		allTask.remove(task);// 执行完任务，销毁
	}

	@Override
	public void onCreate() {
		super.onCreate();
		init();
		registerMessageReceiver();// used for receive msg
		mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		// showNotification();
	}

	// 初始化 JPush。如果已经初始化，但没有登录成功，则执行重新登录。
	private void init() {
		map = new HashMap<String, String>();
		map.put(LOT_ID_ZQ_SFC + "", "胜负彩");
		map.put(LOT_ID_QXC + "", "七星彩");
		map.put(LOT_ID_SSQ + "", "双色球");
		map.put(LOT_ID_FC3D + "", "福彩3D");
		map.put(LOT_ID_QLC + "", "七乐彩");
		map.put(LOT_ID_ZQ_RX9 + "", "任选9");
		map.put(LOT_ID_DLT + "", "大乐透");
		map.put(LOT_ID_BD + "", "北京单场");
		map.put(LOT_ID_PL3 + "", "排列3");
		map.put(LOT_ID_PL5 + "", "排列5");
		map.put(LOT_ID_11X5 + "", "11选5");
		map.put(LOT_ID_11YDJ + "", "11运夺金");
		map.put(LOT_ID_CQSSC + "", "重庆时时彩");
		map.put(LOT_ID_KUAI3 + "", "快三");
		map.put(LOT_ID_JCZQ + "", "竞彩足球");
		map.put(LOT_ID_JCLQ + "", "竞彩篮球");
	}

	public void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction(MESSAGE_RECEIVED_ACTION);
		registerReceiver(mMessageReceiver, filter);
	}

	public class MessageReceiver extends BroadcastReceiver {
		@SuppressLint("SimpleDateFormat") @SuppressWarnings("deprecation")
		@Override
		public void onReceive(Context context, Intent intent) {
        }
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mNM.cancelAll();
	}

	public static void start(Context context) {

		if (false == isServiceRunning(context, MainService.class.getName())) {
			context.startService(new Intent(context, MainService.class));
		} else {
		}
	}

	public static void stop(Context context) {
		context.stopService(new Intent(context, MainService.class));
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	private NotificationManager mNM;

	private final IBinder mBinder = new LocalBinder();

	public class LocalBinder extends Binder {
		public MainService getService() {
			return MainService.this;
		}

		public boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
			// called when client calls transact on returned Binder
			return handleTransactions(code, data, reply, flags);
		}

	}

	/**
	 * 用来判断服务是否运行.
	 * 
	 * @param className
	 *            判断的服务名字
	 * @return true 在运行 false 不在运行
	 */
	public static boolean isServiceRunning(Context mContext, String className) {
		boolean isRunning = false;
		ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(30);
		if (!(serviceList.size() > 0)) {
			return false;
		}
		for (int i = 0; i < serviceList.size(); i++) {
			if (serviceList.get(i).service.getClassName().equals(className) == true) {
				isRunning = true;
				break;
			}
		}
		return isRunning;
	}

	public boolean handleTransactions(int code, Parcel data, Parcel reply, int flags) {
		// TODO Auto-generated method stub
		return false;
	}

}
